<?php

/**
 * purchase actions.
 *
 * @package    Porba
 * @subpackage purchase
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class adminActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
      $this->materials = PartTable::getInstance()->findAll();
           
      $this->setTemplate('index');
  }
  
   public function executeShowSales(){
       $this->version = 'sales'; 
       $this->setTemplate('show');
   }
   
   //first check if there already is a company in the database
   //if there is, put that company in the form, if not create an empty form
   public function executeEditCompany(sfWebRequest $request) {
        $companies = CompanyTable::getInstance()->findAll();
        if($companies->count() == 1){
            foreach($companies as $company){
                $this->form = new CompanyForm($company);
                break;
            }      
        }
        else{
            $this->form = new CompanyForm();
        }           
        $this->setTemplate('edit');
   }
   
   public function executeUpdateCompany(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($company = CompanyTable::getInstance()->find(array($request->getParameter('id'))), sprintf('Object contact does not exist (%s).', $request->getParameter('id')));
        $this->form = new CompanyForm($company);

        $this->processFormCompanyEdit($request, $this->form);

        $this->setTemplate('edit');
    }
    
    protected function processFormCompanyEdit(sfWebRequest $request, sfForm $form){
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $company = $form->save();

            $this->redirect('admin/index');
        }
    }
    
    public function executeCreateCompany(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));
        $this->form = new CompanyForm();

        $this->processFormCompany($request, $this->form);

        $this->setTemplate('edit');
    }
    
    protected function processFormContact(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $company = $form->save();

            $this->redirect('admin/index');
        }
    }
}
?>